﻿using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// 0, 1, 2,
    /// 3, 4, 5,
    /// 6, 7, 8,
    /// </summary>
    /// <param name="posIndex"></param>
    /// <param name="pointInCanvas">发生事件时鼠标相对于画布的位置</param>
    /// <param name="offset"></param>
    public delegate void SizePointPress(int posIndex, Point pointInCanvas, Point offset);

    public delegate void SwitchAreaEventHandler(AreaPart area);

    /// <summary>
    /// Interaction logic for DesignBorder.xaml
    /// </summary>
    public partial class DesignBorder : UserControl
    {
        public event SizePointPress SizePointPress;
        public event SwitchAreaEventHandler SwitchArea;
        private Part _contentPart;

        /// <summary>
        /// 移动锚点的大小
        /// </summary>
        private const double PivotPointSize = 5;
        private const double HalfPivotPointSize = 2.5d;

        public Point Position { get; private set; }

        public Size FullSize { get; private set; }

        public DesignBorder()
        {
            InitializeComponent();
        }

        public void SetTargetPart(Part part)
        {
            this._ctlPartCenter.Width = part.XWidth;
            this._ctlPartCenter.Height = part.XHeight;
            this._contentPart = part;
            //if (part is AreaPart)
            //{
            //    this._ctlBtnEnter.Visibility = System.Windows.Visibility.Visible;
            //}
            //else
            //{
            //    this._ctlBtnEnter.Visibility = System.Windows.Visibility.Hidden;
            //}
        }

        //protected override void OnMouseMove(MouseEventArgs e)
        //{
        //    Debug.WriteLine("mouse move");

        //    base.OnMouseMove(e);
        //}

        public void SetOffset(Point borderOffset)
        {
            Position = new Point(borderOffset.X - PivotPointSize, borderOffset.Y - PivotPointSize);
            //Debug.WriteLine("design border set offset:{0},{1}", borderOffset.X, borderOffset.Y);
            Canvas.SetLeft(this, Position.X);
            Canvas.SetTop(this, Position.Y);
        }

        /// <summary>
        /// 对应的组件的大小，不包含周围的移动点在内
        /// </summary>
        /// <param name="size"></param>
        public void SetSize(Size size)
        {
            FullSize = new Size(size.Width + PivotPointSize * 2, size.Height + PivotPointSize * 2);
            this._ctlBorder.Width = FullSize.Width;
            this._ctlBorder.Height = FullSize.Height;
            this._ctlPartCenter.Width = size.Width;
            this._ctlPartCenter.Height = size.Height;
        }

        public Point GetOffset()
        {
            return new Point(Position.X + PivotPointSize, Position.Y + PivotPointSize); ;
        }

        public Size GetSize()
        {
            return new Size(FullSize.Width - PivotPointSize * 2, FullSize.Height - PivotPointSize * 2);
        }

        /// <summary>
        /// 判断是否点击到了调整大小的点
        /// </summary>
        /// <param name="pointInCanvas"></param>
        /// <param name="canvas"></param>
        /// <returns></returns>
        public DesignBorderHitResult HitDesignBorderTest(Point pointInCanvas, Canvas canvas)
        {
            if (this.Visibility == System.Windows.Visibility.Visible)
            {
                Point pointInThis = canvas.TranslatePoint(pointInCanvas, this._ctlPartCenter);

                if (pointInCanvas.X < Position.X
                    || pointInCanvas.X > Position.X + FullSize.Width
                    && pointInCanvas.Y <= Position.Y
                    && pointInCanvas.Y <= Position.Y + FullSize.Height)
                {
                    //不再范围内
                    return null;
                }

                if (pointInCanvas.X >= Position.X
                    && pointInCanvas.X <= Position.X + PivotPointSize
                    && pointInCanvas.Y >= Position.Y
                    && pointInCanvas.Y <= Position.Y + PivotPointSize)
                {
                    // left top
                    return new DesignBorderHitResult(0, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + FullSize.Width / 2 - HalfPivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width / 2 + HalfPivotPointSize
                    && pointInCanvas.Y >= Position.Y
                    && pointInCanvas.Y <= Position.Y + PivotPointSize)
                {
                    // top
                    return new DesignBorderHitResult(1, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + FullSize.Width - PivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width
                    && pointInCanvas.Y >= Position.Y
                    && pointInCanvas.Y <= Position.Y + PivotPointSize)
                {
                    // right top
                    return new DesignBorderHitResult(2, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X
                    && pointInCanvas.X <= Position.X + PivotPointSize
                    && pointInCanvas.Y >= Position.Y + FullSize.Height / 2 - 2.5
                    && pointInCanvas.Y <= Position.Y + FullSize.Height / 2 + 2.5)
                {
                    // left
                    return new DesignBorderHitResult(3, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + FullSize.Width - PivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width
                    && pointInCanvas.Y >= Position.Y + FullSize.Height / 2 - 2.5
                    && pointInCanvas.Y <= Position.Y + FullSize.Height / 2 + 2.5)
                {
                    // right
                    return new DesignBorderHitResult(5, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X
                    && pointInCanvas.X <= Position.X + PivotPointSize
                    && pointInCanvas.Y >= Position.Y + FullSize.Height - PivotPointSize
                    && pointInCanvas.Y <= Position.Y + FullSize.Height)
                {
                    // left bottom
                    return new DesignBorderHitResult(6, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + FullSize.Width / 2 - HalfPivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width / 2 + HalfPivotPointSize
                    && pointInCanvas.Y >= Position.Y + FullSize.Height - PivotPointSize
                    && pointInCanvas.Y <= Position.Y + FullSize.Height)
                {
                    // bottom
                    return new DesignBorderHitResult(7, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + FullSize.Width - PivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width
                    && pointInCanvas.Y >= Position.Y + FullSize.Height - PivotPointSize
                    && pointInCanvas.Y <= Position.Y + FullSize.Height)
                {
                    // right bottom
                    return new DesignBorderHitResult(8, pointInCanvas, new Point(0, 0), Position);
                }
                else if (pointInCanvas.X >= Position.X + PivotPointSize
                    && pointInCanvas.X <= Position.X + FullSize.Width - PivotPointSize
                    && pointInCanvas.Y >= Position.Y + PivotPointSize
                    && pointInCanvas.Y <= Position.Y + FullSize.Height - PivotPointSize)
                {
                    // center
                    return new DesignBorderHitResult(4, pointInCanvas, pointInThis, Position);
                }
            }
            return null;
        }
    }

    public class DesignBorderHitResult
    {
        public int Direction { get; set; }
        public Point PointInCanvas { get; set; }
        public Point Offset { get; set; }
        /// <summary>
        /// 边框位于canvas的左上角位置
        /// </summary>
        public Point BorderPosition { get; set; }

        public DesignBorderHitResult(int direction, Point pointInCanvas, Point offset, Point borderPosition)
        {
            Direction = direction;
            PointInCanvas = pointInCanvas;
            Offset = offset;
            this.BorderPosition = borderPosition;
        }
    }

    public class ResizeDirection
    {
        public const int Center = 4;
    }
}
